package registry

import (
	"context"
	"mpps/tools/config"

	"github.com/asim/go-micro/plugins/registry/consul/v3"
	"github.com/asim/go-micro/plugins/registry/etcd/v3"
	"github.com/asim/go-micro/v3/registry"
)

type Options struct {
	registryType RegistryType
	Context      context.Context
}

type Option func(*Options)

type RegistryType int

const (
	Etcd RegistryType = iota
	Consul
)

type registryModel struct {
	options Options
}

func RegistryFun(r RegistryType) Option {
	return func(o *Options) {
		o.registryType = r
	}
}

func GetRegistry(opts ...Option) registry.Registry {
	e := &registryModel{
		// 默认etcd
		options: Options{registryType: Etcd},
	}
	for _, o := range opts {
		o(&e.options)
	}
	switch e.options.registryType {
	case Consul:
		return getConsul()
	case Etcd:
		return getEtcd()
	default:
		return getEtcd()
	}
}

func getConsul() registry.Registry {
	return consul.NewRegistry(
	// registry.Addrs(config.ConsulRegAddrs),
	)
}

func getEtcd() registry.Registry {
	return etcd.NewRegistry(
		registry.Addrs(config.G_etcdaddr),
	)
}
